package com.redsoil.house.ui.main.presenter;

import com.redsoil.house.base.RxBus;
import com.redsoil.house.base.RxPresenter;
import com.redsoil.house.model.DataManager;
import com.redsoil.house.model.event.LogoutEvent;
import com.redsoil.house.ui.main.contract.MainContract;
import com.redsoil.house.utils.RxUtil;
import com.redsoil.house.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * 作者：weidingqiang on 2017/9/7 15:39
 * 邮箱：dqwei@iflytek.com
 */

public class MainPresenter extends RxPresenter<MainContract.View> implements MainContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public MainPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(MainContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent(){
        addSubscribe(RxBus.getDefault().toFlowable(LogoutEvent.class)
                        .compose(RxUtil.<LogoutEvent>rxSchedulerHelper())
                        .filter(new Predicate<LogoutEvent>() {
                            @Override
                            public boolean test(@NonNull LogoutEvent logoutEvent) throws Exception {
                                return logoutEvent.getType().equals(LogoutEvent.LOGOUT);
                            }
                        })
                        .subscribeWith(new CommonSubscriber<LogoutEvent>(mView) {
                            @Override
                            public void onNext(LogoutEvent s) {
                                mView.jumpLogin();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                            }
                        })
        );
    }

}
